package com.example.lxy.controller;

import com.alibaba.fastjson.JSON;
import com.example.lxy.pojo.dept;
import com.example.lxy.service.DeptService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;

@Controller
@ResponseBody
@Api(value = "部门管理",tags = "部门接口模块")
public class DeptController {
    @Autowired
    private DeptService deptService;

    @CrossOrigin
    @PostMapping(value = "/api/listdept")
    @ApiOperation(value = "查询所有部门",notes = "从部门表中查询所有")
    public String listdept() {
        HashMap<String, Object> data = new HashMap<>();
        List<dept> list = deptService.list();
        if (list!=null) {
            data.put("flag",200);
            data.put("depts",list);
        } else {
            data.put("flag",400);
        }
        String s = JSON.toJSONString(data);
        return s;
    }

    @CrossOrigin
    @PostMapping(value = "/api/deldept")
    @ApiOperation(value = "删除部门",notes = "根据id删除部门")
    public boolean del(@RequestBody dept d) {
        System.out.println(d.getId());
        boolean dels = deptService.del(d.getId());
        return dels;
    }

    @CrossOrigin
    @PostMapping(value = "/api/upid")
    @ApiOperation(value = "根据ID查询",notes = "根据ID查询部门")
    public dept upid(@RequestBody dept d) {
        dept upid = deptService.upid(d.getId());
        return upid;
    }

    @CrossOrigin
    @PostMapping(value = "/api/update")
    @ApiOperation(value = "修改部门",notes = "根据ID查询出来的信息进行修改")
    public String update(@RequestBody dept d) {
        deptService.update(d);
        return "true";
    }

    @CrossOrigin
    @PostMapping(value = "/api/add")
    @ApiOperation(value = "增加部门",notes = "根据部门表增加部门")
    public String add(@RequestBody dept d) {
        deptService.add(d);
        return "true";
    }
}
